import com.github.pagehelper.PageHelper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class TEST {

    /**
     * 向数据库中插入数据
     * @throws Exception
     */
    @Test
    public void test() throws Exception {
        // 把classpath上的文件转换成输入流
        InputStream inputStream = TEST.class.getClassLoader().getResourceAsStream("data.txt");

        // 使用哪个IO对象比较合适  BufferedReader
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

        List<Address> addresses = new ArrayList<Address>();

        String line = null;

        SqlSession sqlSession = getConnect();
        IAddressDao addressDao = sqlSession.getMapper(IAddressDao.class);

        int i = 1;
        while ( ( line=bufferedReader.readLine() )!=null  ){

            String[] split = line.split(",");
//            System.out.println(Arrays.toString(split));
            Address address = new Address(split[0], split[1], split[2], split[3]);
            addresses.add(address);
//            System.out.println(address);
            if (addresses.size() == 3000){
                addressDao.insert(addresses);
                sqlSession.commit();

                addresses = new ArrayList<Address>();
                System.out.println(i);
                i++;

            }
        }
        if (addresses.size()!=0){
            System.out.println("-------->"+addresses.size());
            addressDao.insert(addresses);
            sqlSession.commit();
        }


        bufferedReader.close();
//        iAddressDao.insert(addresses);
//        sqlSession.commit();
        System.out.println("--执行完毕--");
    }

    public SqlSession getConnect() throws IOException {
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = factory.openSession();
        return sqlSession;
    }

    @Test
    public void getDataFromDataBase() throws Exception{
        BufferedWriter bw = new BufferedWriter(new FileWriter(
                new File("src/main/resources/outData.txt")));
        IAddressDao addressDao = getConnect().getMapper(IAddressDao.class);


        long l = addressDao.selectCount();
        int pageSize = 3000;
        int totalPage = (int) Math.ceil(l*1.0/pageSize);
        System.out.println("一共：" + totalPage + "页");
        for (int i = 1;i<=totalPage;i++){
            //在查询前，设置分页 i第几页  pageSize每页条数
            PageHelper.startPage(i,pageSize);
            List<Address> addressList = addressDao.select();
            for (Address add : addressList){
                bw.write(add.getProvince() + "," + add.getCity() + "," + add.getArea() + "," + add.getDetailed());
                bw.newLine();
                bw.flush();
            }
            System.out.println("第" + i + "页写完了");
//
//            System.out.println("============================");
        }
        bw.close();




    }

}
